package middleware

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"yak.admin/src/module/api/user/repository"
	"yak.admin/src/util"
)

func TokenAuth() gin.HandlerFunc {
	return func(c *gin.Context) {
		token := c.GetHeader("Authorization")
		repos := repository.UserRepository{}
		if user, ok := repos.FindOneByToken(token); ok {
			c.Set(util.UserKey, user)
			c.Next()
			return
		}
		c.Redirect(http.StatusFound, "/api/403")
		c.Abort()
	}
}
